package com.shixun.milktea.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shixun.milktea.entity.Consumer;
import com.shixun.milktea.entity.Manager;
import com.shixun.milktea.entity.Mt;
import com.shixun.milktea.enums.ResultStatusEnum;
import com.shixun.milktea.mapper.ConsumerMapper;
import com.shixun.milktea.service.ConsumerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shixun.milktea.vo.PageVo;
import com.shixun.milktea.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zrs
 * @since 2022-06-07
 */
@Service("smallProgramDetails")
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, Consumer> implements ConsumerService {
    @Autowired
    ConsumerMapper consumerMapper;
    @Override
    public Page<Consumer> selectPage(PageVo pageVo) throws Exception {
        Page<Consumer> page=new Page<>(pageVo.getPageNo()+1, pageVo.getPageSize());
        QueryWrapper<Consumer> qw=new QueryWrapper<>();
        return consumerMapper.selectPage(page,qw);
    }

    @Override
    public Page<Consumer> MtQueryList(PageVo pageVo, String consumerTelephone) throws Exception {
        Page<Consumer> page=new Page<>(pageVo.getPageNo()+1, pageVo.getPageSize());
        QueryWrapper<Consumer> wrapper = new QueryWrapper<>();
        if (consumerTelephone==null||"".equals(consumerTelephone)){
            return consumerMapper.selectPage(page,wrapper);
        }
        wrapper.like("consumer_telephone",consumerTelephone);

        return consumerMapper.selectPage(page,wrapper);
    }

    @Override
    public int register(Consumer consumer) throws Exception {
        String password = consumer.getConsumerPassword();
        String consumerNikename = consumer.getConsumerNikename();
        QueryWrapper<Consumer> wrapper = new QueryWrapper<>();
        wrapper.eq("consumer_nikename",consumerNikename);
        Consumer consumer1 = consumerMapper.selectOne(wrapper);
        if (consumer1!=null){
            return 0;
        }else {
            //加密存储密码
            BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
            String encode = encoder.encode(password);//加密返回密文密码
            consumer.setConsumerPassword(encode);
            return consumerMapper.insert(consumer);
        }



    }

    @Override
    public Consumer loadUserByUsername(String consumerNikename,String consumerPassword) throws Exception {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        Boolean flag=false;
        Consumer userDetails=null;
        try {
            QueryWrapper<Consumer> qw=new QueryWrapper<>();
            QueryWrapper<Consumer> consumer_nikename = qw.eq("consumer_nikename", consumerNikename);
            userDetails = consumerMapper.selectOne(qw);
            System.out.println(encoder.matches(consumerPassword,userDetails.getConsumerPassword()));
            if (consumer_nikename!=null&& encoder.matches(consumerPassword,userDetails.getConsumerPassword())){
                flag= true;
                System.out.println(userDetails);
            }
        } catch (Exception e) {
            throw  new UsernameNotFoundException(ResultStatusEnum.ERROR.getMsg());
        }
        if (flag){
            return userDetails;
        }else {
            throw  new UsernameNotFoundException(ResultStatusEnum.USER_NOT_EXIST.getMsg());
        }


    }

}
